﻿//-----------------------------------------------------------------------
// <copyright file="SettingsPane.cs" company="In The Hand Ltd">
//     Copyright © 2013-14 In The Hand Ltd. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Net;

#if SILVERLIGHT
#if V81
using Windows.Storage;
#else
using InTheHand.Storage;
#endif
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
#else
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
#endif

namespace InTheHand.UI.ApplicationSettings
{
    /// <summary>
    /// A static class that enables the app to control the Settings page.
    /// The app can add or remove commands, receive a notification when the user opens the pane, or open the page programmatically.
    /// </summary>
    public sealed class SettingsPane
    {
        private static SettingsPane instance;

        /// <summary>
        /// Gets a <see cref="SettingsPane"/> object that is associated with the current app.
        /// </summary>
        /// <returns></returns>
        public static SettingsPane GetForCurrentView()
        {
            if (instance == null)
            {
                instance = new SettingsPane();
            }

            return instance;
        }

        internal bool showPublisher = true;

        private SettingsPane()
        {
            
        }

        /// <summary>
        /// Displays the Settings page to the user.
        /// </summary>
        public static void Show()
        {
            if (ApplicationData.Current.LocalSettings.Values.ContainsKey("InTheHand.UI.ApplicationSettings.ShowPublisher"))
            {
                if (!(bool)ApplicationData.Current.LocalSettings.Values["InTheHand.UI.ApplicationSettings.ShowPublisher"])
                {
                    GetForCurrentView().showPublisher = false;
                }
            }

#if SILVERLIGHT
            ((Microsoft.Phone.Controls.PhoneApplicationFrame)Application.Current.RootVisual).Navigate(new Uri("/InTheHand.UI.ApplicationSettings;component/SettingsPage.xaml", UriKind.Relative));
#else
            Frame f = Window.Current.Content as Frame;
            if (f != null)
            {
                f.Navigate(typeof(SettingsPage));
            }
#endif
        }

        internal System.Collections.Generic.IList<SettingsCommand> OnCommandsRequested()
        {
            SettingsPaneCommandsRequestedEventArgs e = new SettingsPaneCommandsRequestedEventArgs();
            try
            {
                if (CommandsRequested != null)
                {
                    CommandsRequested(this, e);
                    return e.Request.ApplicationCommands;
                }
            }
            catch { }

            return null;
        }

        /// <summary>
        /// Occurs when the user opens the settings pane.
        /// Listening for this event lets the app initialize the setting commands and pause its UI until the user closes the pane.
        /// During this event, append your SettingsCommand objects to the available ApplicationCommands vector to make them available to the SettingsPane UI.
        /// </summary>
        [CLSCompliant(false)]
        public event Windows.Foundation.TypedEventHandler<SettingsPane, SettingsPaneCommandsRequestedEventArgs> CommandsRequested;

    }
}
